clear;clc;

% =========================================================================
% 0. LOAD DATA
% =========================================================================

% Step 1: construct variables
  % 1.no fintech
load NoFin_pf75_x75
refi_f0_bf = mean(irf_refi_yp(fini==0,1));
refi_fH_bf = mean(irf_refi_yp(fini==fH,1));
refi_f0_af = mean(irf_refi_ypr(fini==0,1));
refi_fH_af = mean(irf_refi_ypr(fini==fH,1));

pf = sum(fini==fH)/nhh; 
dist0 =[pf 1-pf];

% 2 FinTech w.o. spillovers
load Fin_pq7_q5_pf75_x75.mat 
refi_f0q0_bf = mean(irf_refi_yp(qfini==0,1));
refi_f0q_bf  = mean(irf_refi_yp(qfini==q,1));
refi_fHq0_bf = mean(irf_refi_yp(qfini==fH,1));
refi_fHq_bf  = mean(irf_refi_yp(qfini==fH+q,1));

refi_f0q0_af = mean(irf_refi_ypr(qfini==0,1));
refi_f0q_af  = mean(irf_refi_ypr(qfini==q,1));
refi_fHq0_af = mean(irf_refi_ypr(qfini==fH,1));
refi_fHq_af  = mean(irf_refi_ypr(qfini==fH+q,1));

pf = sum(fini==fH)/nhh; 
pq = sum(qini==q)/nhh;
dist1 =[pf*(1-pq) pf*pq (1-pf)*(1-pq) (1-pf)*pq];

% 3 FinTech w. spillovers (baseline)
load FinE_pq7_q5_pe6_e8.5_pf75_x75.mat 
refi_f0q0e0_bf = mean(irf_refi_yp(qfeini==0,1));
refi_f0qe0_bf  = mean(irf_refi_yp(qfeini==q,1));
refi_f0e_bf    = mean(irf_refi_yp(qfeini==e,1));
refi_fHq0e0_bf = mean(irf_refi_yp(qfeini==fH,1));
refi_fHqe0_bf  = mean(irf_refi_yp(qfeini==fH+q,1));
refi_fHe_bf    = mean(irf_refi_yp(qfeini==fH+e,1));

refi_f0q0e0_af = mean(irf_refi_ypr(qfeini==0,1));
refi_f0qe0_af  = mean(irf_refi_ypr(qfeini==q,1));
refi_f0e_af    = mean(irf_refi_ypr(qfeini==e,1));
refi_fHq0e0_af = mean(irf_refi_ypr(qfeini==fH,1));
refi_fHqe0_af  = mean(irf_refi_ypr(qfeini==fH+q,1));
refi_fHe_af    = mean(irf_refi_ypr(qfeini==fH+e,1));

pf = sum(fini==fH)/nhh; 
pq = sum(qini==q)/nhh;
pe = sum(eini==e)/nhh;
dist2 =[pf*(1-pq)*(1-pe) pf*pq*(1-pe) pf*pe (1-pf)*(1-pq)*(1-pe) (1-pf)*pq*(1-pe) (1-pf)*pe];


% =========================================================================
% Model Figure 2
% =========================================================================

type0 = categorical({'f=f^H','f=f^L'});
type0 = reordercats(type0,{'f=f^H','f=f^L'});

type1 = categorical({'f=f^H,Q=0','f=f^H,Q=1','f=f^L,Q=0','f=f^L,Q=1'});
type1 = reordercats(type1,{'f=f^H,Q=0','f=f^H,Q=1','f=f^L,Q=0','f=f^L,Q=1'});

type2 = categorical({'f=f^H,Q=E=0','f=f^H,Q=1,E=0','f=f^H,E=1','f=f^L,Q=E=0','f=f^L,Q=1,E=0','f=f^L,E=1'});
type2 = reordercats(type2,{'f=f^H,Q=E=0','f=f^H,Q=1,E=0','f=f^H,E=1','f=f^L,Q=E=0','f=f^L,Q=1,E=0','f=f^L,E=1'});


figure;
subplot(3,2,1)
b=bar(type0,100*[refi_fH_bf;refi_f0_bf],0.2);
b.EdgeColor = 'none';
b.FaceColor = 'flat';
b.CData(1,:) = [0, 0.4470, 0.7410];
b.CData(2,:) = [0.8500, 0.3250, 0.0980];
ylim([0 40])
ylabel('Percent','Fontsize',8)
title({'Before MP shock','No-FinTech economy'},'Fontsize',9)

subplot(3,2,2)
b=bar(type0,100*[refi_fH_af;refi_f0_af],0.2);
b.EdgeColor = 'none';
b.FaceColor = 'flat';
b.CData(1,:) = [0, 0.4470, 0.7410];
b.CData(2,:) = [0.8500, 0.3250, 0.0980];
ylim([0 100])
ylabel('Percent','Fontsize',8)
title({'After MP shock','No-FinTech economy'},'Fontsize',9)

subplot(3,2,3)
b=bar(type1,100*[refi_fHq0_bf;refi_fHq_bf;refi_f0q0_bf;refi_f0q_bf],0.4);
b.EdgeColor = 'none';
b.FaceColor = 'flat';
b.CData(1,:) = [0, 0.4470, 0.7410];
b.CData(2,:) = [0.3010, 0.7450, 0.9330];
b.CData(3,:) = [0.8500, 0.3250, 0.0980];
b.CData(4,:) = [0.9290, 0.6940, 0.1250];
ylim([0 40])
ylabel('Percent','Fontsize',8)
title('No-spillover economy')

subplot(3,2,4)
b=bar(type1,100*[refi_fHq0_af;refi_fHq_af;refi_f0q0_af;refi_f0q_af],0.4);
b.EdgeColor = 'none';
b.FaceColor = 'flat';
b.CData(1,:) = [0, 0.4470, 0.7410];
b.CData(2,:) = [0.3010, 0.7450, 0.9330];
b.CData(3,:) = [0.8500, 0.3250, 0.0980];
b.CData(4,:) = [0.9290, 0.6940, 0.1250];
ylim([0 100])
title('No-spillover economy')
ylabel('Percent','Fontsize',8)

subplot(3,2,5)
b=bar(type2,100*[refi_fHq0e0_bf;refi_fHqe0_bf;refi_fHe_bf;refi_f0q0e0_bf;refi_f0qe0_bf;refi_f0e_bf],0.6);
b.EdgeColor = 'none';
b.FaceColor = 'flat';
b.CData(1,:) = [0, 0.4470, 0.7410];
b.CData(2,:) = [0.3010, 0.7450, 0.9330];
b.CData(3,:) = [0.4940, 0.1840, 0.5560];
b.CData(4,:) = [0.8500, 0.3250, 0.0980];
b.CData(5,:) = [0.9290, 0.6940, 0.1250];
b.CData(6,:) = [0.4660, 0.6740, 0.1880];
ylim([0 40])
ylabel('Percent','Fontsize',8)
title('Baseline economy')

subplot(3,2,6)
b=bar(type2,100*[refi_fHq0e0_af;refi_fHqe0_af;refi_fHe_af;refi_f0q0e0_af;refi_f0qe0_af;refi_f0e_af],0.6);
b.EdgeColor = 'none';
b.FaceColor = 'flat';
b.CData(1,:) = [0, 0.4470, 0.7410];
b.CData(2,:) = [0.3010, 0.7450, 0.9330];
b.CData(3,:) = [0.4940, 0.1840, 0.5560];
b.CData(4,:) = [0.8500, 0.3250, 0.0980];
b.CData(5,:) = [0.9290, 0.6940, 0.1250];
b.CData(6,:) = [0.4660, 0.6740, 0.1880];
ylim([0 100])
ylabel('Percent','Fontsize',8)
title('Baseline economy')